package com.bookmarket.controller;
import com.bookmarket.entity.BookName;
import com.bookmarket.service.LJpreviewService;
import com.bookmarket.utils.DocConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/vi")
public class LJpreviewController {
    @Resource
    private LJpreviewService lJpreviewService;


    @RequestMapping("/nlj")
    public ModelAndView nlj(){
        ModelAndView mv=new ModelAndView();
        List<BookName> list=lJpreviewService.selectByBookName();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/upfi")
    public ModelAndView upfi(@RequestParam MultipartFile [] file, HttpSession session, String bookName) throws IOException{
        ModelAndView mv = new ModelAndView();
        String path = session.getServletContext().getRealPath("/uploads");
        /*********************照片*******************************/
        String pictureName=file[1].getOriginalFilename();
        file[1].getContentType();
        File de = new File(path,pictureName);
        /**********************文本******************************/
        String fileName = file[0].getOriginalFilename();
        file[0].getContentType();
        File dest = new File(path,fileName);
        /*******************************************************/
        try {
            //MultipartFile解析上传文件
            file[0].transferTo(dest);
            file[1].transferTo(de);
            /**********************转换**************************************/
            //获取上传文件的文件名
            String extName=fileName.substring(fileName.lastIndexOf(".")+1);
            //文件全路径
            String lastFileName=path+"\\"+fileName;
            //获取需要转换的文件名，将路径名中得\替换为/
            String converfilename=path.replaceAll("\\\\","/")+"/"+fileName;
            //调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法
            DocConverter d=new DocConverter(converfilename);
            //调用conver方法开始转换，先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;
            d.conver();
            //生成swf相对路径，以便传递给flexpaper播放器
            String swfpath="uploads"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));
            //将相对路径放入session中保存
            session.setAttribute("swfpath",swfpath);
            //上传成功后向数据库中插入数据
            System.out.println("poopopop"+pictureName);
            BookName b=new BookName(bookName,swfpath,pictureName);
            lJpreviewService.insertBookName(b);
            /********************跳转页面传路径****************************/
            mv.addObject("swfpath",swfpath);
            mv.setViewName("stage/LJGroup_by");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mv;
    }

    public LJpreviewService getlJpreviewService() {
        return lJpreviewService;
    }

    public void setlJpreviewService(LJpreviewService lJpreviewService) {
        this.lJpreviewService = lJpreviewService;
    }
}
